package com.zbkj.service.settlement;

import com.google.common.collect.Maps;
import com.zbkj.dal.common.exception.CrmebException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 结算工厂
 * @author tianzhonghai
 * 2023/10/6 13:26
 */
@Component
public class SettlementFactory implements InitializingBean {

    @Autowired
    private List<SettleStrategy> settleStrategyList;

    private Map<String, SettleStrategy> stringSettleStrategyMap;

    public SettleStrategy create(String settleType) {
        if (StringUtils.isBlank(settleType)) {
            throw new CrmebException("settleType不能为空");
        }

        return stringSettleStrategyMap.get(settleType);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        stringSettleStrategyMap = Maps.newHashMap();
        for (SettleStrategy settleStrategy : settleStrategyList) {
            Class<?> targetClass = AopUtils.getTargetClass(settleStrategy);
            SettleTypeAnno ruleTypeAnno = AnnotationUtils.getAnnotation(targetClass, SettleTypeAnno.class);
            stringSettleStrategyMap.put(ruleTypeAnno.value(), settleStrategy);
        }
    }
}
